package com.homework.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.homework.bean.CourseBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.ResourceBean;
import com.homework.bean.UserBean;
import com.homework.bean.UserResourceBean;
import com.homework.bean.UserResourceListBean;
import com.homework.service.PageInfoService;
import com.homework.util.connDB;

public class ResourceDao {

	private static ResourceDao resourceDao;
	private connDB conn = new connDB();
	private ResultSet rs;
	private PageInfoService pageInfoService;
	// sql语句
	private String sql = "";

	public ResourceDao() {
		pageInfoService = new PageInfoService();
	}

	public static ResourceDao getInstance() {
		if (resourceDao == null) {
			resourceDao = new ResourceDao();
		}
		return resourceDao;
	}

	// 资源列表
	public UserResourceListBean resourceShow(CourseBean courseBean,
			PageInfoBean pageInfoBean) throws SQLException {
		// 数据库连接
		sql = "select f_username,t_resource.* from t_resource natural join (select f_userName,f_chooseId from t_user natural join (select f_chooseId,f_userId from t_choose where f_courseId = "
				+ courseBean.getCourseId()
				+ ") as t_chooseTemp) as t_userTemp;";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取StudentHomeworkListBean
		UserResourceListBean userResourceListBean = new UserResourceListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			UserResourceBean userResourceBean = new UserResourceBean();
			ResourceBean resourceBean = new ResourceBean();
			UserBean userBean = new UserBean();
			resourceBean.setResourceId(rs.getString("f_resourceId"));
			resourceBean.setResourceName(rs.getString("f_resourceName"));
			resourceBean.setAddress(rs.getString("f_address"));
			resourceBean.setCreationTime(rs.getString("f_creationTime"));
			resourceBean.setDownloadDegree(rs.getString("f_downloadDegree"));
			resourceBean
					.setResourceDescribe(rs.getString("f_resourceDescribe"));
			resourceBean.setResourceStatus(rs.getString("f_resourceStatus"));
			resourceBean.setChooseId(rs.getString("f_chooseId"));
			userBean.setUsername(rs.getString("f_username"));
			userResourceBean.setResourceBean(resourceBean);
			userResourceBean.setUserBean(userBean);
			userResourceListBean.getUserResourceList().add(userResourceBean);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return userResourceListBean;
	}

	// 资源上传
	public void resourceUpload(UserBean userBean, CourseBean courseBean,
			ResourceBean resourceBean) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		// 数据库连接
		sql = "insert into t_resource (f_resourceName,f_address,f_creationTime,f_downloadDegree,f_resourceDescribe,f_resourceStatus,f_chooseId) values ('"
				+ resourceBean.getResourceName()
				+ "','"
				+ resourceBean.getAddress()
				+ "','"
				+ df.format(new Date())
				+ "',0,'"
				+ resourceBean.getResourceDescribe()
				+ "',1,(select f_chooseId from t_choose where f_userId = "
				+ userBean.getUserId()
				+ " and f_courseId = "
				+ courseBean.getCourseId() + "));";
		conn.executeUpdate(sql);
		conn.close();
	}

	// 资源下载
	public String resourceDownload(ResourceBean resourceBean) throws SQLException {
		sql = "select f_address from t_resource where f_resourceId = "+resourceBean.getResourceId()+";";
		rs = conn.executeQuery(sql);
		String address = "";
		if(rs.next()){
			address = rs.getString("f_address");
		}
		rs.close();
		conn.close();
		return address;
	}
}
